package com.kkc.genius.earthquake;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;

import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;

public class EarthQuakeMap extends MapActivity {

	Cursor earthquakeCursor;
	EarthquakeReceiver receiver;
	@Override
	protected void onCreate(Bundle icicle) {
		// TODO Auto-generated method stub
		super.onCreate(icicle);
		setContentView(R.layout.earthquake_map);
		Uri earthquakeURI = EarthquakeProvider.CONTENT_URI;
		earthquakeCursor = getContentResolver().query(earthquakeURI, null,	null, null, null	);
		
		MapView earthquakeMap = (MapView)findViewById(R.id.map_view);
		EarthquakeOverlay eo = new EarthquakeOverlay(earthquakeCursor);
		earthquakeMap.getOverlays().add(eo);		
	}
	
	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		earthquakeCursor.requery();
		
		IntentFilter filter;
		filter = new IntentFilter(EarthquakeService.NEW_EARTHQUAKE_FOUND);
		receiver = new EarthquakeReceiver();
		registerReceiver(receiver, filter);
		super.onResume();
	}
	
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		earthquakeCursor.deactivate();
		super.onPause();
	}
	
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		earthquakeCursor.close();
		super.onDestroy();
	}
	
	public class EarthquakeReceiver extends BroadcastReceiver {
		@Override
		public void onReceive(Context context, Intent intent) {
			earthquakeCursor.requery();
			MapView earthquakeMap = (MapView)findViewById(R.id.map_view);
			earthquakeMap.invalidate();
		}
	}
}
